<?php
# autoHTML: the dynamic PHP5 library for fast website prototyping
# http://code.google.com/p/autohtml/
#
# Copyright (c) 2010, Shu Zong Chen
# http://freelanceDreams.com/
# All rights reserved. 
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met: 
# 
#  * Redistributions of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer. 
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution. 
#  * Neither the name of autoHTML nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
namespace fd\autoserver;

class RESPONSE{
	const RESPONSE100 = '100 Continue';
	const RESPONSE101 = '101 Switching Protocols';
	const RESPONSE200 = '200 OK';
	const RESPONSE201 = '201 Created';
	const RESPONSE202 = '202 Accepted';
	const RESPONSE203 = '203 Non-AUthoritative Information';
	const RESPONSE204 = '204 No Content';
	const RESPONSE205 = '205 Reset Content';
	const RESPONSE206 = '206 Partial Content';
	const RESPONSE300 = '300 Multiple Choices';
	const RESPONSE301 = '301 Moved Permanently';
	const RESPONSE302 = '302 Found';
	const RESPONSE303 = '303 See Other';
	const RESPONSE304 = '304 Not Modified';
	const RESPONSE305 = '305 Use Proxy';
	const RESPONSE307 = '307 Temporary Redirect';
	const RESPONSE400 = '400 Bad Request';
	const RESPONSE401 = '401 Unauthorized';
	const RESPONSE402 = '402 Payment Required';
	const RESPONSE403 = '403 Forbidden';
	const RESPONSE404 = '404 Not Found';
	const RESPONSE405 = '405 Method Not Allowed';
	const RESPONSE406 = '406 Not Acceptable';
	const RESPONSE407 = '407 Proxy AUthentication Required';
	const RESPONSE408 = '408 Request Timeout';
	const RESPONSE409 = '409 Conflict';
	const RESPONSE410 = '410 Gone';
	const RESPONSE411 = '411 Length Required';
	const RESPONSE412 = '412 Precondition Failed';
	const RESPONSE413 = '413 Request Entity Too Large';
	const RESPONSE414 = '414 Request-URI Too Long';
	const RESPONSE415 = '415 Unsupported Media Type';
	const RESPONSE416 = '416 Requested Range Not Satisfiable';
	const RESPONSE417 = '417 Expectation Failed';
	const RESPONSE500 = '500 Internal Server Error';
	const RESPONSE501 = '501 Not Implemented';
	const RESPONSE502 = '502 Bad Gateway';
	const RESPONSE503 = '503 Unavailable';
	const RESPONSE504 = '504 Gateway Timeout';
	const RESPONSE505 = '505 HTTP Version Not Supported';
	const TYPE_HTML = 'text/html';
	protected $responseCode;
	protected $responseType = self::TYPE_HTML;
	protected $responseLength = 0;
	protected $response = null;
	protected $headerOnly = false;
	protected $queue;

	function __construct(){
		$this->init();
	}
	function init(){
		return;
	}
	function setCode($code){
		$this->responseCode = $code;
	}
	function getHead(){
		return 'HTTP/1.1 '. $this->responseCode . "\r\n";
	}
	function getType(){
		return 'Content-Type: ' . $this->responseType . "\r\n";
	}
	function getDate(){
		return 'Date: ' . gmdate('r');
	}
	function getLength(){
		return 'Content-Length: ' . $this->responseLength . "\r\n";
	}
	function getBlank(){
		return "\r\n";
	}
	function setResponse($str){
		$this->response = $str;
		$this->responseLength = strlen($str);
	}
	function getResponse(){
		return $this->response;
	}
	function formulateResponse(){
		if ($this->responseCode === null){
			die('Why didn\'t you set a response code?');
		}
		$this->queue[] = $this->getHead();
		if ($this->responseType !== null){
			$this->queue[] = $this->getType();
		}
		$this->queue[] = $this->getDate();
		if ($this->headerOnly || $this->response === null){
			$this->queue[] = $this->getBlank();
			return;
		}
		$this->queue[] = $this->getLength();
		$this->queue[] = $this->getBlank();
		$this->queue[] = $this->getResponse();
		$this->queue[] = $this->getBlank();
	}
	function getQueue(){
		return $this->queue;
	}
}

class RESPONSE_404 extends RESPONSE{
	protected $responseCode = self::RESPONSE404;
	function init(){
		$this->setResponse('<html><body><h2>Error 404</h2><p>Something not found.</p></body></html>');
	}
}

class RESPONSE_MISSINGHOST extends RESPONSE{
	protected $responseCode = self::RESPONSE400;
	function init(){
		$this->setResponse('<html><body><h2>No Host: header received</h2>HTTP 1.1 requests must include the Host: header.</body></html>');
	}
}

class RESPONSE_FAKE extends RESPONSE{
	protected $responseCode = self::RESPONSE200;
	function init(){
		$this->setResponse('<html><body><h2>OKAY!</h2></body></html>');
	}
}